package Model;

import Entity.User;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.RegularExpression;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ResourceBundle;
import javax.swing.table.AbstractTableModel;

public class UserModel extends AbstractTableModel{
    ArrayList<User> listUser;
    ResourceBundle message;
    RegularExpression reg;
   
    public UserModel(ArrayList<User> listUser, ResourceBundle message){
        this.listUser = listUser;
        this.message = message;
    }
    public boolean AddUser(User u){
        for (Iterator<User> it = listUser.iterator(); it.hasNext();) {
            User user = it.next();
            if(u.getUserName().compareToIgnoreCase(user.getUserName()) == 0)
                return false;
        }
        listUser.add(u);
        return true;
    }
    public ArrayList<User> getListUser(){
        return (ArrayList<User>)listUser.clone();
    }
    public boolean setList(ArrayList<User> listUser){
        try {
            this.listUser = listUser;
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    public boolean checkUserName(String userName){
        if(userName == "" || userName.length() >25)
            return false;
        String pattern = message.getString("patternUserName");
        reg = new RegularExpression(pattern);
        return reg.matches(userName);
    }
    public boolean checkLastName(String name){
        String pattern = message.getString("patternLastName");
        reg = new RegularExpression(pattern);
        return reg.matches(name);
    }
    public boolean checkFirstName(String name){
        String pattern = message.getString("patternFirstName");
        reg = new RegularExpression(pattern);
        return reg.matches(name);
    }
    
    public boolean checkEmail(String email){
        String pattern = message.getString("patternEmail");
        reg = new RegularExpression(pattern);
        return reg.matches(email);
    }
    
    public boolean checkPhone(String phone){
        String pattern = message.getString("patternPhone");
        reg = new RegularExpression(pattern);
        return reg.matches(phone);
    }
    public User findUser(int userID){
        for (Iterator<User> it = listUser.iterator(); it.hasNext();) {
            User u  = it.next();
            if(u.getId() == userID)
                return u;
        }
        return null;
    }
    public int totalUser(){
        return listUser.size();
    }
    @Override
    public int getRowCount() {
        return listUser.size();
    }

    @Override
    public int getColumnCount() {
        return 8;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        User user = listUser.get(rowIndex);
        switch(columnIndex){
            case 0: return user.getId();
            case 1: return user.getUserName();
            case 2: {
                int priority = user.getPriority();
                if(priority == 0)
                    return "Admin";
                else if(priority == 1)
                    return "Saler";
                else if(priority == 2)
                    return "Inventory Manager";
                else if(priority == 3)  
                    return "Sale Manager";  

            }
            case 3: return user.getFirstName() + user.getLastName();
            case 4: if(user.isGender()) return "Male";
                    else return "Female";
            case 5: return user.getEmail();
            case 6: return user.getBirthday();
            case 7: return user.getPhone();
            default:return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch(columnIndex){
            case 0: return Integer.class;
            case 1: return String.class;
            case 2: return String.class;
            case 3: return String.class;
            case 4: return String.class;
            case 5: return String.class;
            case 6: return Date.class;
            case 7: return String.class;
            default: return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch(column){
            case 0: return message.getString("userIDD");
            case 1: return message.getString("userUserName");
            case 2: return message.getString("userPriority");
            case 3: return message.getString("userFullName");
            case 4: return message.getString("userGender");
            case 5: return message.getString("userEmail");
            case 6: return message.getString("userBirthday");
            case 7: return message.getString("userPhone");
            default: return null;
        }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }
}
